﻿using System;
using System.Net;
using System.IO;
using System.Xml;
using System.Text;
using System.Security.Authentication;

namespace LiveNet.Contacts
{
    public sealed partial class LiveContacts
    {
        private delegate void ResponseCompletedDelegate(string xml);

        private void MakeGetRequest(Uri uri, ResponseCompletedDelegate response)
        {
            HttpWebRequest request = BuildRequest(uri, "GET");
            m_uri = request.RequestUri;

            try
            {
                using (HttpWebResponse httpresponse = (HttpWebResponse)request.GetResponse())
                {
                    using (Stream stream = httpresponse.GetResponseStream())
                    {
                        m_xml = new StreamReader(stream).ReadToEnd();
                        response(m_xml);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new LiveNetRequestException(ex.Message + ": request=" + request.RequestUri.ToString() + ", xml=" + m_xml, ex);
            }
        }

        private void MakePutRequest(Uri uri, string body)
        {
            HttpWebRequest request = BuildRequest(uri, "PUT");
            m_uri = request.RequestUri;
            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] data = encoding.GetBytes(body);

            // Set the content length of the string being posted.
            request.ContentLength = data.Length;
            request.ContentType = "application/xml; charset=utf-8";

            Stream newStream = request.GetRequestStream();
            newStream.Write (data, 0, data.Length);
            newStream.Close ();

            m_xml = body;

            try
            {
                using (HttpWebResponse httpresponse = (HttpWebResponse)request.GetResponse())
                {
                    using (Stream stream = httpresponse.GetResponseStream())
                    {
                    }
                }
            }
            catch (Exception ex)
            {
                throw new LiveNetRequestException(ex.Message + ": request=" + request.RequestUri.ToString() + ", xml=" + m_xml, ex);
            }
        }

        private void MakePostRequest(Uri uri, string body)
        {
            HttpWebRequest request = BuildRequest(uri, "POST");
            m_uri = request.RequestUri;
            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] data = encoding.GetBytes(body);

            // Set the content length of the string being posted.
            request.ContentLength = data.Length;
            request.ContentType = "application/xml; charset=utf-8";

            Stream newStream = request.GetRequestStream();
            newStream.Write(data, 0, data.Length);
            newStream.Close();

            m_xml = body;

            try
            {
                using (HttpWebResponse httpresponse = (HttpWebResponse)request.GetResponse())
                {
                    using (Stream stream = httpresponse.GetResponseStream())
                    {
                    }
                    m_location = httpresponse.Headers["Location"];
                }
            }
            catch (Exception ex)
            {
                throw new LiveNetRequestException(ex.Message + ": request=" + request.RequestUri.ToString() + ", xml=" + m_xml, ex);
            }
        }

        private void MakeDeleteRequest(Uri uri)
        {
            HttpWebRequest request = BuildRequest(uri, "DELETE");
            m_uri = uri;
            m_xml = "";
            
            try
            {
                using (HttpWebResponse httpresponse = (HttpWebResponse)request.GetResponse())
                {
                    using (Stream stream = httpresponse.GetResponseStream())
                    {
                    }
                }
            }
            catch (Exception ex)
            {
                throw new LiveNetRequestException(ex.Message + ": request=" + request.RequestUri.ToString() + ", xml=" + m_xml, ex);
            }
        }
    }
}
